package cn.icanci.ladybird.service.user.impl;

import cn.icanci.ladybird.dao.UserBuyerTicketDao;
import cn.icanci.ladybird.pojo.bo.BuyerTicketBO;
import cn.icanci.ladybird.pojo.dto.UserBuyerTicketDTO;
import cn.icanci.ladybird.pojo.entity.BuyerTicket;
import cn.icanci.ladybird.pojo.entity.UserInfoBuyerTicketMiddle;
import cn.icanci.ladybird.service.BuyerTicketService;
import cn.icanci.ladybird.service.UserInfoBuyerTicketMiddleService;
import cn.icanci.ladybird.service.UserInfoService;
import cn.icanci.ladybird.service.user.UserBuyerTicketService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: zxp
 * @date: Created in 2020/10/3 0003 下午 19:16
 * @classAction: 常用购票人
 */
@SuppressWarnings("all")
@Service
public class UserBuyerTicketServiceImpl implements UserBuyerTicketService {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserInfoBuyerTicketMiddleService userInfoBuyerTicketMiddleService;
    @Autowired
    private BuyerTicketService buyerTicketService;
    @Autowired
    private UserBuyerTicketDao userBuyerTicketDao;


    @Override
    public List<UserBuyerTicketDTO> queryUserBuyerTicketDTOByUserInfoId(Long userInfoId) {
        // 根据用户的id查询所有的购票人
        List<UserInfoBuyerTicketMiddle> ticketMiddles = userInfoBuyerTicketMiddleService.queryByUserInfoId(userInfoId);
//        List<UserInfoBuyerTicketMiddle> ticketMiddles = userInfoBuyerTicketMiddleService.queryAllByLimit(0,8);
        List<UserBuyerTicketDTO> list = new ArrayList<UserBuyerTicketDTO>();
        for (UserInfoBuyerTicketMiddle ticketMiddle : ticketMiddles) {
            Long userId = ticketMiddle.getBuyerTicketMiddleId();
            BuyerTicket buyerTicket = buyerTicketService.queryById(userId);
            if (null != buyerTicket) {
                // 查询用户的信息
                UserBuyerTicketDTO userBuyerTicketDTO = new UserBuyerTicketDTO();
                userBuyerTicketDTO.setUserInfoId(1L);
                //设置证件类型
                userBuyerTicketDTO.setBuyerTicketDocumentType(buyerTicket.getBuyerTicketDocumentType());
                //设置购票人姓名
                userBuyerTicketDTO.setBuyerTicketUsername(buyerTicket.getBuyerTicketUsername());
                //设置证件号
                userBuyerTicketDTO.setBuyerTicketIdCardNumber(buyerTicket.getBuyerTicketIdCardNumber());
                //设置购票人id
                userBuyerTicketDTO.setBuyerTicketId(buyerTicket.getBuyerTicketId());
                list.add(userBuyerTicketDTO);
            }
        }

        return list;
    }

    @Override
    public PageInfo<UserBuyerTicketDTO> queryAllByNotObject(Long userInfoId, Integer pageNum, Integer pageSize) {
        // 分页
        // pageNum：当前页数   pageSize：当前页需要显示的数量
        // 只要有一个没有传值，那么默认就是首页，然后设置为首页
        if (null == pageNum || null == pageSize) {
            pageNum = 1;
            pageSize = 6;
        }
        // 写在查询的前面
        PageHelper.startPage(pageNum, pageSize);
        List<UserBuyerTicketDTO> userBuyerTicketDTOS = this.userBuyerTicketDao.queryAllByNotObject(userInfoId);
        // 把查询的结果封装到 PageInfo
        PageInfo<UserBuyerTicketDTO> userBuyerTicketDTOPageInfo = new PageInfo<>(userBuyerTicketDTOS);
        return userBuyerTicketDTOPageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long buyTicketId) {
        UserInfoBuyerTicketMiddle userInfoBuyerTicketMiddle = userInfoBuyerTicketMiddleService.queryByTicketId(buyTicketId);
        userInfoBuyerTicketMiddleService.deleteByTicketId(buyTicketId);
        buyerTicketService.deleteById(buyTicketId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertById(BuyerTicketBO buyerTicketBO) {
        BuyerTicket buyerTicket = new BuyerTicket();
        buyerTicket.setBuyerTicketIdCardNumber(buyerTicketBO.getBuyerTicketIdCardNumber());
        buyerTicket.setBuyerTicketUsername(buyerTicketBO.getBuyerTicketUsername());
        buyerTicket.setBuyerTicketDocumentType(buyerTicketBO.getBuyerTicketDocumentType());
        buyerTicket.setCreateTime(new Date());
        buyerTicket.setUpdateTime(new Date());
        buyerTicket.setBuyerTicketEnabled(1);
        buyerTicketService.insert(buyerTicket);
        System.err.println(buyerTicket);
        //插入自增的id
        Long buyerTicketId = buyerTicket.getBuyerTicketId();
        System.err.println(buyerTicketId);
        //插入中间表
        //创建中间表对象
        UserInfoBuyerTicketMiddle userInfoBuyerTicketMiddle = new UserInfoBuyerTicketMiddle();
        userInfoBuyerTicketMiddle.setUserInfoMiddleId(buyerTicketBO.getUserInfoId());
        userInfoBuyerTicketMiddle.setBuyerTicketMiddleId(buyerTicketId);
        userInfoBuyerTicketMiddle.setCreateTime(new Date());
        userInfoBuyerTicketMiddle.setUpdateTime(new Date());
        System.err.println(userInfoBuyerTicketMiddle);
        //插入
        userInfoBuyerTicketMiddleService.insert(userInfoBuyerTicketMiddle);
    }
}
